package com.woniushop.dao.imple;

import java.util.List;

import com.woniushop.dao.BaseDAO;
import com.woniushop.dao.UserDAO;
import com.woniushop.entity.User;

/**
 * 用户DAO的实现类
 * 
 * @author 崔新雄
 *
 */
public class UserDAOImple implements UserDAO {

	private BaseDAO<User> baseDAO = new BaseDAO<User>();

	/**
	 * 将user对象加入数据库中
	 * 
	 * @param user
	 */
	@Override
	public void add(User user) {
		String sql = "insert into user(uaccount,uname,upsw,urole,ustate,ucartid) values(?,?,?,?,?,?)";
		Object[] objs = { user.getUaccount(), user.getUname(), user.getUpsw(), user.getUrole(), user.getUstate(),
				user.getUcartid() };
		baseDAO.update(sql, objs);
	}

	/**
	 * 根据用户id删除数据库中对应的用户数据
	 * 
	 * @param uid
	 */
	@Override
	public void delete(Integer uid) {
		String sql = "delete from user where uid=?";
		Object[] objs = { uid };
		baseDAO.update(sql, objs);
	}

	/**
	 * 修改对应的用户数据
	 * 
	 * @param user
	 */
	@Override
	public void update(User user) {
		String sql = "update user set uaccount=?,uname=?,upsw=?,urole=?,ustate=?,ucartid=? where uid=?";
		Object[] objs = { user.getUaccount(), user.getUname(), user.getUpsw(), user.getUrole(), user.getUstate(),
				user.getUcartid(), user.getUid() };
		baseDAO.update(sql, objs);
	}

	/**
	 * 根据对应的用户id查找用户数据
	 * 
	 * @param uid
	 * @return
	 */
	@Override
	public User findOne(Integer uid) {
		String sql = "select * from user where uid=?";
		Object[] objs = { uid };
		List<User> list = baseDAO.select(sql, User.class, objs);
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	/**
	 * 根据对应的用户账号查找用户数据
	 * 
	 * @param uaccount
	 * @return
	 */
	@Override
	public User findOne(String uaccount) {
		String sql = "select * from user where uaccount=?";
		Object[] objs = { uaccount };
		List<User> list = baseDAO.select(sql, User.class, objs);
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	/**
	 * 根据条件分页查询用户数据
	 * 
	 * @param page
	 * @param pageSize
	 * @param user
	 * @return
	 */
	@Override
	public List<User> findByPage(Integer page, Integer pageSize, User user) {
		String sql = "select * from user where 1=1";
		sql = finishSQL(user, sql);
		sql += " limit ?,?";
		return baseDAO.select(sql, User.class, (page - 1) * pageSize, pageSize);
	}

	/**
	 * 根据条件获得用户数据总数
	 * 
	 * @return
	 */
	@Override
	public Integer count(User user) {
		String sql = "select count(*) from user where 1=1";
		sql = finishSQL(user, sql);
		return baseDAO.count(sql);
	}

	private String finishSQL(User user, String sql) {
		String uaccount = user.getUaccount();
		if (uaccount != null && !uaccount.trim().equals("")) {
			sql += " and uaccount like '%" + uaccount + "%'";
		}
		String uname = user.getUname();
		if (uname != null && !uname.trim().equals("")) {
			sql += " and uname like '%" + uname + "%'";
		}
		Integer ustate = user.getUstate();
		if (ustate != null) {
			sql += " and  ustate='" + ustate + "'";
		}
		Integer urole = user.getUrole();
		if (urole != null) {
			sql += " and  urole='" + urole + "'";
		}
		return sql;
	}

}
